package com.appspot.didit.server;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gwt.user.client.ui.FormPanel;

public class ImageByteReader extends HttpServlet {

        private static final long serialVersionUID = -8873813591069236947L;
        
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    		resp.sendRedirect("/");
    		return;
    	}
        
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
                try {
                        String variable = request.getParameter("var");
                        HttpSession session = request.getSession();
                        byte[] imageBytes = (byte[]) session.getAttribute(variable);
                        response.reset();
                        int contentLength = imageBytes.length;
                        response.setContentLength(contentLength);
                        response.setContentType("application");
                        response.setHeader("Content-Disposition", "attachment; filename=chart.png");
                        BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
                        output.write(imageBytes);
                        output.flush();
                        output.close();
                }
                catch (Exception e) {}
        }
        
        protected void test(HttpServletRequest request, HttpServletResponse response) throws ServletException {
            try {
				//String strout = request.getParameter("var");
            	
            	String fileName = "Export.csv";
            	response.reset();
            	response.setContentType( "application/vnd.csv"); 
                //set header as attachment
            	response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 
            	BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
            	PrintWriter out;
        		out = response.getWriter();
        		//out.write(strout);
            }
            catch (Exception e) {}
    }
}
